define(function(require, exports, module) {
    'use strict';
    
    var rk = require('rk');
    require('./titleTab');
    require('../pages/platform_schedule-index-eventEditor');
    require('./tipApi');
    var scheduleUtil = require('oa/js/schedule/asset/utils/scheduleUtil');


    $.widget("rk.eventEditorWindow", $.extend(true, {}, $.superClass.tipApi, {
        options: {},
        tabObj: null,
        _create: function() {
            var me = this;
            var elem = me.element;

            me.tabObj = elem.find('div[data-plugin="titleTab"]');
            me.tabObj.titleTab($.extend({}, me.options)); //tab
            me.tabObj.find('a[class*="close_schedule"]').click(function(evt) { //close button
                evt.preventDefault();
                scheduleUtil.closeEditor();
            });

            var act = me.options.act;
            var eventType = me.options.eventType;

            //act='edit';
            if (act == 'edit') {
                if (eventType == 'task') {
                    me.tabObj.titleTab('select', 1);
                }
                var pop_head = me.tabObj.find('.pop_head');
                pop_head.addClass('schedule_editing_head').find('.schedule_head').hide();
            }

            var opt = {
                closeMe: function() {
                    scheduleUtil.closeEditor();
                },
                scheduleSucc: function(json) {
                    if(me.options.success){
                        me.options.success(json);
                    }
                    scheduleUtil.loadEvents();

                    //日程左拉点击编辑
                    if($("a[act=createSchedule]").size() > 0) {
                        $('#oa_panel').schedulePanelCtrl('reload');
                    } else if($("a[act=modify_schedule]").size() > 0) {
                        //日程最终页：重刷最终页
                        window.location.reload();
                    } else if($("a[act=createSchedule_today]").size() > 0) {
                        //今天工作：新增日程或任务
                        require.async('page/js/index/todaywork/todayWorkCtrl.js', function() {
                            var $taskPanel = $('#index_task_panel');

                            //重新加载今天工作
                            $taskPanel.todayWorkCtrl();
                        });
                    }
                },
                scheduleFail: function() {
                    alert(rk.i18n('REQUEST_ERROR'))
                },
                taskSucc: function(json) {
                    if(me.options.success){
                        me.options.success(json);
                    }
                    scheduleUtil.loadEvents();

                    //日程左拉点击编辑
                    if($("a[act=createSchedule]").size() > 0) {
                        $('#oa_panel').schedulePanelCtrl('reload');
                    } else if($("a[act=modify_schedule]").size() > 0) {
                        //日程最终页：重刷最终页
                        window.location.reload();
                    } else if($("a[act=createSchedule_today]").size() > 0) {
                        //今天工作：新增日程或任务
                        require.async('page/js/index/todaywork/todayWorkCtrl.js', function() {
                            var $taskPanel = $('#index_task_panel');

                            //重新加载今天工作
                            $taskPanel.todayWorkCtrl();
                        });
                    }
                },
                taskFail: function() {
                    alert(rk.i18n('REQUEST_ERROR'))
                }
            };
            elem.find('div[data-plugin="scheduleEditController"]').scheduleEditController($.extend({}, opt, me.options));
            if(act == "create"){
                elem.find('div[data-plugin="taskEditController"]').taskEditController($.extend({}, opt, me.options));
            }
        },
        setData: function(data) {
            var me = this;
            var elem = me.element;
            elem.find('div[data-plugin="scheduleEditController"]').scheduleEditController('resetView');
            elem.find('div[data-plugin="taskEditController"]').taskEditController('resetView');
        }
    }));
});